﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV.Forms.KhaiBao;
using System.Data.SqlClient;

namespace QLTV.UserControl
{
    public partial class ucRank : UserControlBase
    {
        public ucRank()
        {
            InitializeComponent();
        }

        private void treeList1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                popupMenu1.ShowPopup(new Point(Cursor.Position.X, Cursor.Position.Y));
            }
        }

        private void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmEditRank f = new frmEditRank();
            f.Text = "Thêm chức danh";
            f.AddClick += f_AddClick;
            f.ShowDialog();
        }

        void f_AddClick(object sender, EventArgs e)
        {
            this.tblRankTableAdapter.Fill(dsRanking.tblRank);
        }

        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (viewRank.FocusedRowHandle>-1)
            {
                int position = this.tblRankBindingSource.Position;
                frmEditRank f = new frmEditRank(this.tblRankBindingSource);
                f.Text = "Sửa chức danh";
                f.ShowDialog();
                this.tblRankTableAdapter.Fill(dsRanking.tblRank);
                this.tblRankBindingSource.Position = position;
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //XtraMessageBox.Show(rm.GetString("sConfirm", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes
            if (XtraMessageBox.Show(((BaseForm)ParentForm).rm.GetString("sConfirm", ((BaseForm)ParentForm).myCultureInfo), ((BaseForm)ParentForm).rm.GetString("sMessageCaption", ((BaseForm)ParentForm).myCultureInfo), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    this.tblRankBindingSource.RemoveCurrent();
                    this.tblRankTableAdapter.Update(dsRanking.tblRank);
                    this.tblRankTableAdapter.Fill(dsRanking.tblRank);
                }
                catch (SqlException ex)
                {
                    if (ex.ErrorCode == -2146232060)
                    {
                        XtraMessageBox.Show(((BaseForm)ParentForm).rm.GetString("sDataInUsed", ((BaseForm)ParentForm).myCultureInfo), ((BaseForm)ParentForm).rm.GetString("sMessageCaption", ((BaseForm)ParentForm).myCultureInfo), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        XtraMessageBox.Show(ex.Message, ((BaseForm)ParentForm).rm.GetString("sMessageCaption", ((BaseForm)ParentForm).myCultureInfo), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

        }
        private void checkPermission()
        {
            btnThem.Enabled = MyPermission.Them;
            btnSua.Enabled = MyPermission.Sua;
            btnXoa.Enabled = MyPermission.Xoa;
        }
        private void ucRank_Load(object sender, EventArgs e)
        {
            
            this.tblRankTableAdapter.Fill(dsRanking.tblRank);
            tblCrewTypeTableAdapter.Fill(dsRanking.tblCrewType);
            checkPermission();
        }

 
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmEditRank f = new frmEditRank();
            f.Text = "Thêm chức danh";
            f.AddClick += f_AddClick;
            f.ShowDialog();
            this.tblRankTableAdapter.Fill(dsRanking.tblRank);
            
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (viewRank.FocusedRowHandle >-1)
            {
                int position = this.tblRankBindingSource.Position;
                frmEditRank f = new frmEditRank(this.tblRankBindingSource);
                f.Text = "Sửa chức danh";
                f.ShowDialog();
                this.tblRankTableAdapter.Fill(dsRanking.tblRank);
                this.tblRankBindingSource.Position = position;
            }
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //XtraMessageBox.Show(rm.GetString("sConfirm", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes
            if (XtraMessageBox.Show(((BaseForm)ParentForm).rm.GetString("sConfirm", ((BaseForm)ParentForm).myCultureInfo), ((BaseForm)ParentForm).rm.GetString("sMessageCaption", ((BaseForm)ParentForm).myCultureInfo), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    this.tblRankBindingSource.RemoveCurrent();
                    this.tblRankTableAdapter.Update(dsRanking.tblRank);
                    this.tblRankTableAdapter.Fill(dsRanking.tblRank);
                }
                catch (SqlException ex)
                {
                    if (ex.ErrorCode == -2146232060)
                    {
                        XtraMessageBox.Show(((BaseForm)ParentForm).rm.GetString("sDataInUsed", ((BaseForm)ParentForm).myCultureInfo), ((BaseForm)ParentForm).rm.GetString("sMessageCaption", ((BaseForm)ParentForm).myCultureInfo), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        XtraMessageBox.Show(ex.Message, ((BaseForm)ParentForm).rm.GetString("sMessageCaption", ((BaseForm)ParentForm).myCultureInfo), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

     

        private void viewRank_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle > -1)
            {
                this.tblCertificationTableAdapter.FillByRankId(dsCertificate.tblCertification, int.Parse(viewRank.GetFocusedRowCellValue(colnRankId).ToString()));
            }
        }

        private void viewRank_DoubleClick(object sender, EventArgs e)
        {

            if (MyPermission.Sua)
            {
                if (viewRank.FocusedRowHandle > -1)
                {
                    int position = this.tblRankBindingSource.Position;
                    frmEditRank f = new frmEditRank(this.tblRankBindingSource);
                    f.Text = "Sửa chức danh";
                    f.ShowDialog();
                    this.tblRankTableAdapter.Fill(dsRanking.tblRank);
                    this.tblRankBindingSource.Position = position;
                }
            }
        }
    }
}
